package com.bridgeintelligent.tag.webserver.dim.mgmt.service.append;

import com.bridgeintelligent.tag.webserver.dim.mgmt.api.DimRep;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@Api(tags = "历史数据追加新标签")
@RestController
@RequestMapping("api/append")
@Slf4j
public class DimAppendController {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @Autowired
    private ObjectMapper objectMapper;

    @ApiOperation(value = "读数据文件追加新标签数据", notes = "读数据文件追加新标签数据")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "dimId", value = "标签ID", required = true),
            @ApiImplicitParam(paramType = "query", name = "filePath", value = "文件路径(文件在bulkload服务器上的绝对地址)", required = true)
    })
    @GetMapping("/file")
    public DimRep append(String dimId,String filePath) {
        DimAppendMqMsg dimAppendMqMsg = new DimAppendMqMsg()
                .setDimId(dimId)
                .setFilePath(filePath)
                .setType(0);
        try {
            String msg = objectMapper.writeValueAsString(dimAppendMqMsg);
            rocketMQTemplate.convertAndSend("DIM_APPEND_TOP", msg);
        } catch (JsonProcessingException e) {
            log.error(dimId+"--------》》》追加标签发送MQ失败"+e);
            return new DimRep(-1,"追加标签发送MQ失败");
        }
        return new DimRep(0);
    }

    @ApiOperation(value = "新增mapping映射字段", notes = "新增mapping映射字段")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "dimId", value = "标签ID", required = true),
            @ApiImplicitParam(paramType = "query", name = "classify", value = "标签类型", required = true)
    })
    @GetMapping("/mapping")
    public DimRep mapping(String dimId,String classify) {
        DimAppendMqMsg dimAppendMqMsg = new DimAppendMqMsg()
                .setDimId(dimId)
                .setClassify(classify);
        try {
            String msg = objectMapper.writeValueAsString(dimAppendMqMsg);
            rocketMQTemplate.convertAndSend("MAPPING_APPEND_TOP", msg);
        } catch (JsonProcessingException e) {
            log.error(dimId+"--------》》》新增mapping映射字段发送MQ失败"+e);
            return new DimRep(-1,"新增mapping映射字段发送MQ失败");
        }
        return new DimRep(0);
    }


}
